<template  >
  <v-form v-model="valid" ref="mypurchaseForm">
    <v-container grid-list-md>
      <v-layout wrap>
        <v-flex>
          <v-text-field v-model="purchase.lsh" label="单据编码" disabled></v-text-field>
        </v-flex>
        <v-flex>
          <v-select
            :disabled="isEdit"
            v-model="purchase.lx"
            item-text="text"
            item-value="value"
            :items="items"
            label="类型"
            required
          ></v-select>
        </v-flex>
        <v-flex>
          <v-select
            :disabled="isEdit"
            v-model="purchase.storeCode"
            item-text="storeName"
            item-value="storeCode"
            :items="stores"
            @change="change"
            label="店铺"
            required
          />
        </v-flex>
        <v-flex v-if="isEdit" xs12 sm6>
          <v-text-field v-model="purchase.truename" label="录入人：" hint disabled></v-text-field>
        </v-flex>
        <v-flex v-if="isEdit" xs12 sm6>
          <v-text-field v-model="purchase.lrsj" label="录入时间" hint persistent-hint></v-text-field>
        </v-flex>
      </v-layout>
      <formtable v-if="!isck" ref="datatable" v-model="purchase.list" :datasource="purchase.list"></formtable>

      <formtable1 v-if="!!isck" ref="datatable" v-model="purchase.list" :datasource="purchase.list"></formtable1>
    </v-container>

    <v-layout class="my-4" row>
      <v-spacer />
      <v-btn v-if="!purchase.lsh || purchase.status === 0 " @click="submit" color="primary">保存</v-btn>
      <v-btn v-if="!purchase.lsh" @click="clear" color="primary">重置</v-btn>
      <v-btn v-if="purchase.lsh && purchase.status === 0" @click="commit" color="primary">提交</v-btn>
      <v-btn v-if="purchase.lsh &&  purchase.status === 0" @click="del" color="primary">作废</v-btn>

      <v-btn v-if="purchase.status===1 && !!showck" color="primary" @click="shenhe(1)">审核</v-btn>
      <v-btn v-if="purchase.status===2 && !!showck" color="primary" @click="shenhe(2)">审核</v-btn>
    </v-layout>
  </v-form>
</template>
<script>
import formtable from "./ReturnTable";
import formtable1 from "./ReturnTable1";
export default {
  name: "return-form",
  props: {
    oldpurchase: {
      type: Object
    },
    isEdit: {
      type: Boolean,
      default: false
    },
    showck: {
      type: Boolean,
      default: false
    },
    isck: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      valid: false, // 表单校验结果标记
      purchase: {
        lsh: "", // 路线首字母
        id: "", // 路线logo
        lrid: "", // 路线logo
        statusMsg: 0, // 路线所属的商品分类数组
        list: [],
        lx: "",
        lrsj: "",
        stores: []
      },
      nameRules: [
        v => !!v || "路线名称不能为空",
        v => v.length > 1 || "路线名称至少2位"
      ],
      checkbox: false,
      items: [
        { text: "正品", value: 0 },
        { text: "样机", value: 1 },
        { text: "赠品", value: 2 },
        { text: "残次", value: 3 }
      ],
      stores: []
    };
  },
  methods: {
    getDataFromServer() {
      this.$http.get("/item/user/store").then(res => {
        this.stores = res.data;
      });
    },
    shenhe(a) {
      const { ...params } = this.purchase;
      params.username = window.localStorage.getItem("username");
      params.lrid = window.localStorage.getItem("id");
      params.truename = window.localStorage.getItem("truename");
      params.list = this.$refs.datatable.datasource;

      this.$http({
        method: "post",
        url: a === 1 ? "/item/fc/shenhe1" : "/item/fc/shenhe2",
        data: JSON.stringify(params),
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        }
      })
        .then(() => {
          // 关闭窗口
          this.$emit("close");
          this.$message.success("保存成功！");
        })
        .catch(() => {
          this.$message.error("保存失败！");
        });
    },

    submit() {
      // 表单校验
      if (this.$refs.mypurchaseForm.validate()) {
        // 定义一个请求参数对象，通过解构表达式来获取purchase中的属性
        const { ...params } = this.purchase;
        params.username = window.localStorage.getItem("username");
        params.lrid = window.localStorage.getItem("id");
        params.truename = window.localStorage.getItem("truename");
        // params.deptCode = window.localStorage.getItem("deptCode");
        // params.storeCode = window.localStorage.getItem("storeCode");
        params.list = this.$refs.datatable.datasource;

        if ([0, 1, 2, 3].indexOf(this.purchase.lx) < 0) {
          this.$message.error("请选择类型");
          return;
        }

        if (!this.purchase.storeCode) {
          this.$message.error("请选择店铺");
          return;
        }

        this.$http({
          method: "post",
          url: this.isEdit ? "/item/fc/modify" : "/item/fc/insert",
          data: JSON.stringify(params),
          headers: {
            "Content-Type": "application/json;charset=UTF-8"
          }
        })
          .then(() => {
            // 关闭窗口
            this.$emit("close");
            this.$message.success("保存成功！");
          })
          .catch(() => {
            this.$message.error("保存失败！");
          });
      }
    },
    commit() {
      const { ...params } = this.purchase;
      params.username = window.localStorage.getItem("username");
      params.lrid = window.localStorage.getItem("id");
      params.truename = window.localStorage.getItem("truename");
      params.list = this.$refs.datatable.datasource;

      this.$http({
        method: "post",
        url: "/item/fc/commit",
        data: JSON.stringify(params),
        headers: {
          "Content-Type": "application/json;charset=UTF-8"
        }
      })
        .then(() => {
          // 关闭窗口
          this.$emit("close");
          this.$message.success("保存成功！");
        })
        .catch(() => {
          this.$message.error("保存失败！");
        });
    },
    change() {},
    del() {
      // 表单校验
      if (this.$refs.mypurchaseForm.validate()) {
        // 定义一个请求参数对象，通过解构表达式来获取purchase中的属性
        const { ...params } = this.purchase;
        params.username = window.localStorage.getItem("username");
        params.lrid = window.localStorage.getItem("id");
        // params.lx = window.localStorage.getItem("lx");
        params.truename = window.localStorage.getItem("truename");
        params.list = [];

        this.$http({
          method: "post",
          url: "/item/fc/delete",
          data: JSON.stringify(params),
          headers: {
            "Content-Type": "application/json;charset=UTF-8"
          }
        })
          .then(() => {
            // 关闭窗口
            this.$emit("close");
            this.$message.success("作废成功！");
          })
          .catch(() => {
            this.$message.error("作废失败！");
          });
      }
    },
    clear() {
      // 重置表单
      this.$refs.mypurchaseForm.reset();
    }
  },
  watch: {
    showck: {
      handler(val) {
        this.showck = val;
      }
    },
    oldpurchase: {
      // 监控oldBrand的变化
      handler(val) {
        this.getDataFromServer();
        if (val) {
          // 注意不要直接复制，否则这边的修改会影响到父组件的数据，copy属性即可
          this.purchase = Object.deepCopy(val);
        } else {
          // 为空，初始化brand
          this.purchase = {
            name: "",
            code: "",
            id: "",
            status: 0,
            list: [],
            lx: "",
            stores: []
          };
        }
      },
      deep: true
    }
  },
  components: {
    formtable,
    formtable1
  }
};
</script>

<style scoped>
</style>
